<template>
  <div>
    <el-table
      ref="dataTable"
      :data="dataSource.list || []"
      :height="getHeight(options.topHeight)"
      :stripe="options.stripe"
      :border="options.border"
      @row-click="handleRowClick"
      @selection-change="handleSelectionChange"
      header-row-class-name="table-header-row"
      highlight-current-row
    >
      <!--selection选择框-->
      <el-table-column
        v-if="options.mutiSelect"
        type="selection"
        style="width:50px"
        align="center"
      ></el-table-column>
      <!--序号-->
      <el-table-column
        v-if="options.index"
        label="序号"
        type="index"
        width="80"
        :index="indexMethod"
        align="center"
      ></el-table-column>

      <!--数据列-->
      <template v-for="(column, index) in columns">
        <el-table-column
          v-if="!column.hide"
          :key="index"
          :prop="column.prop"
          :label="column.label"
          :align="column.align || 'left'"
          :width="column.width"
          :fixed="column.fixed"
        >
          <template slot-scope="scope">
            <template v-if="!column.render">
              {{ scope.row[column.prop] }}
            </template>
            <!-- render -->
            <template v-else>
              <RenderDom
                :row="scope.row"
                :index="index"
                :render="column.render"
              />
            </template>

            <!-- render button -->
            <template v-if="column.button">
              <el-button-group>
                <template v-for="(btn, i) in column.group">
                  <el-button
                    :key="i"
                    :type="btn.type"
                    :size="btn.size || 'mini'"
                    :icon="btn.icon"
                    :disabled="btn.disabled"
                    :plain="btn.plain"
                    @click.stop="btn.onClick(scope.row, scope.$index)"
                    >{{ btn.name }}</el-button
                  >
                </template>
              </el-button-group>
            </template>
            <!-- slot 你可以其他常用项 -->
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="dataSource.totalCount"
      background
      :total="dataSource.totalCount"
      :page-sizes="[15, 30, 50, 100]"
      :page-size="dataSource.pageSize"
      :current-page.sync="dataSource.pageNo"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleIndexChange"
      style="text-align: right"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  components: {
    RenderDom: {
      functional: true, // 函数式组件 - 无 data 和 this 上下文 => better render
      props: {
        row: Object,
        index: Number,
        render: Function,
      },
      /**
       * @param {Function} createElement - 原生创建dom元素的方法， 弃用，推荐使用 jsx
       * @param {Object} ctx - 渲染的节点的this对象
       * @argument 传递参数 row index
       */
      render(createElement, ctx) {
        const { row, index } = ctx.props
        return ctx.props.render(row, index)
      },
    },
  },
  props: {
    dataSource: Object,
    options: Object, // 表格参数控制 maxHeight、stripe 等等...
    columns: Array,
    fetch: Function, // 获取数据的函数
    initFetch: {
      type: Boolean,
      default: true,
    },
  },
  created() {
    console.log(this.options)
    // 传入的options覆盖默认设置
    this.$parent.tableOptions = Object.assign(
      {
        stripe: false, // 是否为斑马纹
        border: false,
      },
      this.options
    )
    if (this.initFetch) {
      this.fetch()
    }
  },
  methods: {
    getHeight(topPanelHeight) {
      if (this.options.height !== undefined) {
        return this.options.height
      }

      topPanelHeight =
        topPanelHeight == undefined
          ? this.global.topPanelHeight
          : topPanelHeight
      return this.getTableHeight(topPanelHeight)
    },

    handleSizeChange(size) {
      // 切换每页显示的数量
      this.dataSource.pageSize = size
      this.dataSource.pageNo = 1
      this.fetch()
    },
    handleIndexChange(pageNo) {
      // 切换页码
      this.dataSource.pageNo = pageNo
      this.fetch()
    },
    handleSelectionChange(selection) {
      this.$emit('selectionChange', selection)
    },
    handleRowClick(row, event, column) {
      this.$emit('row-click', row, event, column)
    },
    indexMethod(index) {
      const pageSize = this.dataSource.pageSize || 1
      let pageNo = this.dataSource.pageNo || 1
      pageNo = pageNo == 0 ? 1 : pageNo
      return (pageNo - 1) * pageSize + index + 1
    },
    setCurrentRow(row) {
      this.$refs.dataTable.setCurrentRow(row)
    },
  },
}
</script>
<style lang="less" scoped>
.el-pagination {
  box-sizing: border-box;
  padding: 10px 10px 0px 10px;
}
</style>
